package com.wenjh.jdmzt;

import java.io.File;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Environment;

import com.wenjh.jdmzt.service.dao.DBOpenHandler;
import com.wenjh.jdmzt.service.model.Preference;
import com.wenjh.jdmzt.service.model.User;
import com.wenjh.jdmzt.util.Log4Android;
import com.wenjh.jdmzt.util.SharedPreferencesUtil;
import com.wenjh.jdmzt.util.Toaster;
import com.wenjh.jdmzt.R;

public class MApplication extends Application {
	public User currentUser = null;
	public Preference preference;
	private SQLiteDatabase db = null;
	public SharedPreferencesUtil publicPreference;
	public String token = "";
	
	Log4Android log = new Log4Android(getClass().getSimpleName());
	
	@Override
	public void onCreate() {
		super.onCreate();
		AndroidKit.onApplicationCreate(this);
		Toaster.doEnable(getApplicationContext());
		publicPreference = SharedPreferencesUtil
				.getInstance(this, Preference.PUBLIC_PREFERENCE_FILE_NAME);
	}
	
	public synchronized SQLiteDatabase getSqliteInstance() {
		if (db == null || !db.isOpen()) {
			DBOpenHandler dbOpenHandler = new DBOpenHandler(this);
			db = dbOpenHandler.getWritableDatabase();
			db.execSQL("PRAGMA cache_size=10240; ");
		}

		return db;
	}
	
	@TargetApi(Build.VERSION_CODES.FROYO)
	@Override
	public File getExternalCacheDir() {
		if(AndroidKit.getSDKVersion() < 7) {
			return new File(Environment.getExternalStorageDirectory(), "Android/data/"+getPackageName()+"/cache");
		} else {
			return super.getExternalCacheDir();
		}
	}
	
}
